#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'arrow'
__explain__ = ''

import datetime

from django.conf import settings
from moviepy.video.VideoClip import ImageClip, TextClip
from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
from moviepy.video.fx.resize import resize
from moviepy.video.io.VideoFileClip import VideoFileClip

from av_editor.models import MaterialFile, FILE_FINISHED
from common.constants import VIDEO_TYPE
from common.file_utils import custom_file_path, create_dir


def make_t2_video(file_path, left_image_path, bottom_image_path):
    video_clip = VideoFileClip(file_path)
    size = video_clip.size
    duration = video_clip.duration
    # 视频缩小
    video_clip = resize(video_clip, 0.8)
    # 设置视频的位置和背景
    video_clip = video_clip.on_color(size, (255, 255, 255), pos=(size[0]*0.2, 0))
    # 左图
    left_image_clip = ImageClip(left_image_path, duration=duration).set_position((142, 142))
    left_image_clip = resize(left_image_clip, (100, 100))
    # 下图
    bottom_image_clip = ImageClip(bottom_image_path, duration=duration).set_position((142, 280))
    bottom_image_clip = resize(bottom_image_clip, (100, 100))
    # 字幕
    text1 = TextClip("VideoCarrier", fontsize=60, color="black", font="WenQuanYi-Zen-Hei", method='caption',
                    ).set_duration(duration).set_position((10,10))
    text2 = TextClip("ERC20协议是什么", fontsize=60, color="black", font="Noto-Sans-CJK-JP-Bold", method='caption',
                     ).set_duration(duration).set_position(("center", 922))
    final_clip = CompositeVideoClip([video_clip, left_image_clip, bottom_image_clip, text1, text2])
    # filename = f"V{datetime.datetime.now().strftime('%Y%m%d%H%M%s')}.mp4"
    filename = f"V{datetime.datetime.now().strftime('%Y%m%d%H%M%s')}.png"
    save_file_path = custom_file_path("self", filename)
    # 生成的视频的绝对地址
    generate_file_path = settings.MEDIA_ROOT + "/" + save_file_path
    create_dir(generate_file_path)
    final_clip.save_frame(generate_file_path, t=10)
    # final_clip.write_videofile(generate_file_path)
    # MaterialFile.objects.create(file=save_file_path, file_type=VIDEO_TYPE, name=filename, source="self",
    #                             status=FILE_FINISHED, note="自动生成", suffix="mp4")


def make_t1_video(file_path, left_image_path, bottom_image_path):
    video_clip = VideoFileClip(file_path)
    size = video_clip.size
    duration = video_clip.duration
    # 视频缩小
    video_clip = resize(video_clip, 0.65)
    # 设置视频的位置和背景
    video_clip = video_clip.on_color(size, (255, 255, 255), pos=(size[0]*0.4, 0))
    # 左图
    left_image_clip = ImageClip(left_image_path, duration=duration).set_position(("left", "center"))
    left_image_clip = resize(left_image_clip, (size[0]*0.3, size[1]*0.8))
    # 下图
    bottom_image_clip = ImageClip(bottom_image_path, duration=duration).set_position(("center", "bottom"))
    bottom_image_clip = resize(bottom_image_clip, (88, 88))
    # 字幕
    text = TextClip("VideoCarrier", fontsize=60, color="black", font="WenQuanYi-Zen-Hei", method='caption',
                    ).set_duration(duration).set_position((10,10))
    final_clip = CompositeVideoClip([video_clip, left_image_clip, bottom_image_clip, text])
    # filename = f"V{datetime.datetime.now().strftime('%Y%m%d%H%M%s')}.mp4"
    filename = f"V{datetime.datetime.now().strftime('%Y%m%d%H%M%s')}.png"
    save_file_path = custom_file_path("self", filename)
    # 生成的视频的绝对地址
    generate_file_path = settings.MEDIA_ROOT + "/" + save_file_path
    create_dir(generate_file_path)
    final_clip.save_frame(generate_file_path, t=10)
    # final_clip.write_videofile(generate_file_path)
    # MaterialFile.objects.create(file=save_file_path, file_type=VIDEO_TYPE, name=filename, source="self",
    #                             status=FILE_FINISHED, note="自动生成", suffix="mp4")
